package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;

import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service

public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult pageSelectSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVO = setmealMapper.pageSelectSetmeal(setmeal);
        return new PageResult(setmealVO.getTotal(), setmealVO.getResult());
    }

    @Override
    public SetmealVO byIdSelectSetmeal(Long id) {
        SetmealVO setmealVO = setmealMapper.byIdSelectSetmeal(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.bySetmealIdSelect(setmealVO.getId());
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.updateSetmeal(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        List<Long> ids = new ArrayList<>();
        ids.add(setmealDTO.getId());
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        setmealDishMapper.deleteSetmealDish(ids);
        setmealDishMapper.insertSetmealDish(setmealDishes);
    }

    @Override
    public void statusStartOrStop(Long id, Integer status) {
        List<SetmealDish> setmealDishes = setmealDishMapper.bySetmealIdSelect(id);
        setmealDishes.forEach(a -> {
                DishVO dishVO = dishMapper.selectDishById(a.getDishId());
                if (dishVO.getStatus() == StatusConstant.DISABLE) {
                    throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
        });
        setmealMapper.updateSetmeal(Setmeal.builder().id(id).status(status).build());
    }


    @Override
    @Transactional
    public void insertSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertSetmeal(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(a -> {
            a.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.insertSetmealDish(setmealDishes);
    }

    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        ids.forEach(id -> {
            SetmealVO setmealVO = setmealMapper.byIdSelectSetmeal(id);
            if (setmealVO.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        setmealMapper.deleteSetmeal(ids);
        setmealDishMapper.deleteSetmealDish(ids);
    }

}
